<?php
//后台登陆控制
include_once('admin_global.php');
$db->Get_user_shell_check($uid, $shell);   //检测用户是否登录
$db->Get_user_type_check($uid, $shell);    //检测用户权限
//先显示出已有栏目分类
$query=$db->select("category","*","");
 //将查询结果存储到数组中
 while($row=$db->fetch_array($query)){
    $data[]=$row;
}
$tree = new Tree($data);       //树形类实例化
$gettree = $tree->get_tree(0, "<option value=\$id >\$spacer\$cateName</option>", 0);      //得到树形结构
//查询出栏目显示风格
$query=$db->select("catestyle","*","isEnabled=1");
 //将查询结果存储到数组中
 while($row=$db->fetch_array($query)){
    $styleResult[] = $row;
}
//添加栏目分类
if(isset($_POST['addCate'])){
    //同级栏目名称不能重复
    $catePID=$_POST['pid'];
    $cateName=trim($_POST['cateName']);
    $query=$db->select('category',"*","cateName='$cateName' AND catePID=$catePID");
    $num=$db->db_num_rows();
    if($num>0){
        echo "<script>alert('该栏目已存在！');history.go(-1);</script>";
    }else{
        //一级导航栏目不能是单页栏目
        $cateStyleID = $_POST['cateStyleID'];
        if ($catePID == 0 && $cateStyleID == 2) {
            echo "<script>alert('一级导航栏目不能作为单页栏目！');history.go(-1);</script>";
        }else{
            //新建栏目不能作为单页栏目的子栏目
            $query=$db->select('category',"*","id=$catePID");
            $reslut=$db->fetch_array();
            if($reslut['cateStyleID']==2){
                echo "<script>alert('父栏目不能为单页栏目！');history.go(-1);</script>";
            }else{
                //符合要求，准备新建栏目
                if (!empty($_POST['isShow'])) {
                    $isShow = 1;
                } else {
                    $isShow = 0;
                }
                $cateNav = $_POST['cateNav'];
                $cateIndex=intval($_POST['cateIndex']);
                $cateCreateDate = date("Y-m-d");
                $cateRemark = $_POST['cateRemark'];
                $rows=$db->insert("category","id,catePID,cateName,isShow,cateStyleID,cateIndex,cateNavigation,cateCreateDate,cateRemark","null,$catePID,'$cateName',$isShow,$cateStyleID,$cateIndex,'$cateNav','$cateCreateDate','$cateRemark'");
                if($rows>0){
                    echo "<script>alert('栏目添加成功！');window.location.href='listCate.php'</script>";
                }else{
                    echo "<script>alert('栏目添加失败！');history.go(-1);</script>";
                }
            }
        }
    }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>添加栏目</title>
        <link rel="stylesheet" href="css/right.css" type="text/css" /> 
        <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
        <script type="text/javascript" src="js/jquery.validate-1.13.1.js"></script>
    </head>
    <script>
        $(function(){
            $("#form1").validate({
                rules:{
                    cateName:{
                        required:true,
                    },
                    cateIndex:{
                        required:true,
                        number:true,
                        range:[0,99]
                    }
                },
                messages:{
                    cateName:{
                        required:"请填写栏目名称！",
                    },
                    cateIndex:{
                        required: "请填写栏目的排序值！",
                        number: "栏目排序要求是数值！",
                        range: "栏目排序数值的范围在0~99之间！"
                    }
                }
            })
        })
    </script>
    <body>
        <div id="right_main">
            <div class="right_content">
                <div class="right_top_menu">
                    <h3>
                        您当前的位置：后台 > 栏目管理 > 添加栏目
                    </h3>
                </div>
                <div class="right_center_content">
                    <form id="form1" method="post" action="">
                        <table cellpadding="0" cellspacing="0" border="0" width="100%">
                            <tr>
                                <td valign="top" >
                                    <div class="txl_search"></div>
                                    <table cellpadding="0" cellspacing="0" border="0" width="100%"  class="sy_table">
                                        <tr>
                                            <th colspan="2" >添加栏目分类</th>
                                        </tr>
                                        <tr>
                                            <td width="35%" align="right">所属栏目：</td>
                                            <td width="65%">
                                                <select name="pid">
                                                    <option value="0" >无（作为一级栏目）</option> 
                                                    <?php print_r($gettree); ?>
                                                </select>
                                            </td>
                                        </tr> 
                                        <tr>
                                            <td width="35%" align="right">栏目名称：</td>
                                            <td width="65%"><input type="text" name="cateName"  class="int_text" value="" size="40" /></td>
                                        </tr>
                                        <tr>
                                            <td width="35%" align="right">栏目说明：</td>
                                            <td width="65%"><input type="text" name="cateRemark"  class="int_text" value="" size="40" />&nbsp;&nbsp;可以不填</td>
                                        </tr> 
                                        <tr>
                                            <td width="35%" align="right">栏目风格：</td>
                                            <td width="65%">
                                                <select name="cateStyleID">
                                                <?php foreach ($styleResult as $row){; ?>
                                                    <option value="<?php echo $row['cateStyleID'] ?>"><?php echo $row['cateStyleName'] ?></option> 
                                                <?php }; ?>
                                                </select> * 栏目风格是指栏目在前台页面显示时的外观样式，默认为文章列表样式，单页栏目请选择“单页内容”，教师风采等以图片为主的栏目请选择图片列表。
                                            </td>
                                        </tr>
                                        <tr>
                                            <td width="35%" align="right">栏目位置：</td>
                                            <td width="65%">
                                                <select name="cateNav">
                                                    <option value="主导航" selected="selected">主导航</option> 
                                                    <option value="侧导航">侧导航</option> 
                                                </select>
                                            </td>
                                        </tr> 
                                        <tr>
                                            <td width="35%" align="right">栏目排序：</td>
                                            <td width="65%"><input type="text" name="cateIndex"  class="int_text" value="0" size="10" /> &nbsp;值为0-99，值越大，越靠前</td>
                                        </tr>   
                                        <tr>
                                            <td width="35%" align="right">是否显示：</td>
                                            <td width="65%"><input type="checkbox" name="isShow"  class="int_text"  checked="checked" value="6" size="40" /></td>
                                        </tr> 
                                        <tr>
                                            <td colspan="2" align="center" height='30'>
                                                <input type="submit" class="btn_ipt01" name="addCate" value=" 添加栏目 "/>
                                            </td>  
                                        </tr>
                                    </table>
                                </td>
                            </tr>   
                        </table>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>
